<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>原型继承</title>
	<!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
</head>
<body>
<script>
	`use strict`;
	function Student(props) {
		this.name = props.name || 'Unnamed';
	}

	Student.prototype.hello = function () {
		alert('Hello, ' + this.name + '!');
	};

	//基于Student扩展出PrimaryStudent
	/**
	 * 但是，调用了Student构造函数不等于继承了Student，PrimaryStudent创建的对象的原型是：
	 * new PrimaryStudent() ----> PrimaryStudent.prototype ----> Object.prototype ----> null
	 * 必须想办法把原型链修改为：
	 * new PrimaryStudent() ----> PrimaryStudent.prototype ----> Student.prototype ----> Object.prototype ----> null
	 */
	function PrimaryStudent(props) {
		Student.call(this,props);
		this.grade = props.grade;
	}
	//空函数F
	function F() {

	}
	//把F的原型指向Student.prototype
	F.prototype = Student.prototype;
	//把PrimaryStudent的原型指向一个性的F对象
	PrimaryStudent.prototype = new F();
	//把PrimaryStudent的构造函数修复为PrimaryStudent
	PrimaryStudent.prototype.constructor = PrimaryStudent;
	//继续在PrimaryStudent的原型上定义方法
	PrimaryStudent.prototype.getGrade = function () {
		return this.grade;
	}

	let xiaoming = new PrimaryStudent({
		name:'小明',
		grade:3
	});
	console.log("xiaoming.name:" + xiaoming.name);
	console.log("xiaoming.grade:" + xiaoming.grade);
	//原型验证
	console.log(xiaoming.__proto__ === PrimaryStudent.prototype);
	console.log(xiaoming.__proto__.__proto__ === Student.prototype);
	//验证继承关系
	console.log(xiaoming instanceof PrimaryStudent);
	console.log(xiaoming instanceof Student);
	/**
	 * 注意，函数F仅用于桥接，我们仅创建了一个new F()实例，而且，没有改变原有的Student定义的原型链。
	 */
</script>
</body>
</html>